﻿using AbpApplication.InPut;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyControl.Tools.Extension;
using NewWms.Apiservice;
using NewWms.Apiservice.Dto;
using NewWms.Apiservice.InPut;
using NewWms.Extend;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using Reconstruct_Desktop.Apiservice.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using WcsZz.Extend;
using wms_wpf.Apiservice;
using wms_wpf.Extend;

namespace NewWms.ViewModels
{
    public partial class InventorydetailsViewModel : BindableBase, INavigationAware
    {
        //define
        private readonly IViewService_Client viewService;
        private readonly IBaseService_Client baseService;
        private readonly IEssService_Client essService;
        private Prism.Services.Dialogs.IDialogService Dialog;
        public InventorydetailsViewModel(Prism.Services.Dialogs.IDialogService dialog)
        {
            viewService = new ViewService_Client();
            baseService = new BaseService_Client();
            essService = new EssService_Client();
            InPut_ViewService9 = new InPut_ViewService9_Client();
            Command = new DelegateCommand<string>(Method);
            View_Material_WarehouseLineDtos = new List<View_Material_WarehouseLineDto>();
            InPut_ViewService9.PageIndex = 1;
            InPut_ViewService9.PageSize = 50;
            this.Dialog = dialog;
        }
        private long count;
        public long Count
        {
            get { return count; }
            set { count = value; RaisePropertyChanged(); }
        }
        private long pagecount;
        public long PageCount
        {
            get { return pagecount; }
            set { pagecount = value; RaisePropertyChanged(); }
        }
        public DelegateCommand<string> Command { get; set; }
        private List<View_Material_WarehouseLineDto> view_Material_WarehouseLineDtos;
        public List<View_Material_WarehouseLineDto> View_Material_WarehouseLineDtos
        {
            get { return view_Material_WarehouseLineDtos; }
            set { view_Material_WarehouseLineDtos = value; RaisePropertyChanged(); }
        }
        private InPut_ViewService9_Client inPut_ViewService9;
        public InPut_ViewService9_Client InPut_ViewService9
        {
            get { return inPut_ViewService9; }
            set { inPut_ViewService9 = value; RaisePropertyChanged(); }
        }
        public void Method(string obj)
        {
            switch (obj)
            {
                case "View_Material_WarehouseLine":
                    View_Material_WarehouseLine();break;
    
            }
        }

        //箱盒视图
        [RelayCommand]
        private async void View_Material_WarehouseLine()
        {
        
            Tuple<List<View_Material_WarehouseLineDto>, long?, long?> tuple = await viewService.View_Material_WarehouseLine(InPut_ViewService9);
            if (tuple.Item1 != null)
            {
                View_Material_WarehouseLineDtos = tuple.Item1;
                Count = (long)tuple.Item2;
                PageCount = (long)tuple.Item3;
            }
        }
        //下架物料
        [RelayCommand]
        private async void CreateNeetOutMaterial(System.Collections.IList SelectItems)
        {
            //if (SelectItems.Count > 20)
            //{
            //    Growl.Info( "一次最多出20个","msg");
            //    return;
            //}
            
            InPut_BaseService10_Client inPut_BaseService10=new InPut_BaseService10_Client();
            inPut_BaseService10.InlethouseCode=GlobalVariable.InletCode;
            foreach (View_Material_WarehouseLineDto item in SelectItems)
            {
                if (item.Islock == true)
                {
                    Growl.Info("有被出库锁定的物料", "msg");
                    return;
                }
                inPut_BaseService10.MaterialuniqueCodes.Add(item.MaterialuniqueCode);

            }
            inPut_BaseService10.Machine = int.Parse( GlobalVariable.Machine);
            Tuple<bool,List<TaskOrderDto>> tuple = await baseService.CreateNeetOutMaterial(inPut_BaseService10);
            if (tuple.Item1)
            {
                EventAggregatorHelper.EventAggregator.SendMessage("修改成功", "跳转页面");
                View_Material_WarehouseLine();
            }


        }
        //导出库存
        [RelayCommand]
        private async void Export()
        {
            InPut_ViewService9.PageIndex = 1;
            InPut_ViewService9.PageSize = 100000;
            Tuple<List<View_Material_WarehouseLineDto>, long?, long?> tuple = await viewService.View_Material_WarehouseLine(InPut_ViewService9);
            if (tuple.Item1 != null)
            {
                ExportToExcel.ExportToExcelMethod(tuple.Item1);

                System.Windows.MessageBox.Show("导出成功");
            }
  
        }
        //修改手动出库数量
        [RelayCommand]
        private async void UpdatManualQuantity(View_Material_WarehouseLineDto Dto)
        {
            bool boolres= int.TryParse(Dto.Poundage, out _);//_什么意思
            if (!boolres)
            {
                Growl.Info("请输入数字", "msg");
                View_Material_WarehouseLine();
                return;
            }
            InPut_BaseService24_Client inPut_BaseService24_Client = new InPut_BaseService24_Client();
            inPut_BaseService24_Client.Id_Materialbase = Dto.Id_Materialbase;
            inPut_BaseService24_Client.Poundage = Dto.Poundage;
            bool res= await baseService.UpdatManualQuantity(inPut_BaseService24_Client);
            if (!res)
            {
                View_Material_WarehouseLine();
            }
        }
        //盘库
        [RelayCommand]
        private async void StockTaking()
        {
           MessageBoxResult messageBoxResult=    HandyControl.Controls.MessageBox.Info("注意:1.物料盘库时只能在无出库任务时执行 2.盘库时如未有任务生成则代表未查询到此料/或者此料未在库3.盘库任务只能单次执行只有任务执行完毕才能执行下一批");
            if (messageBoxResult == MessageBoxResult.OK)
            {
                Dialog.ShowDialog("StockTakingDialog");
            }
               
        }
        


        //主页设置
        private string title = "在库明细查询";
        public string Title
        {
            get { return title; }
            set { title = value; RaisePropertyChanged(); }
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
           
        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            View_Material_WarehouseLine();
        }
    }
}
